
package coffee;

import coffee.listeners.NewMessageEvent;
import coffee.listeners.NewMessageListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author bwolff
 */
public class CoffeeModel {
    
    CoffeeConnector c;
    private ServerMessage answer;
    
    public CoffeeModel() {
        c = new CoffeeConnector();
    }
    
    public void removeNewMessageListener(NewMessageListener listener) {
        c.readStream.removeNewMessageListener(listener);
    }
    
    public void addNewMessageListener(NewMessageListener listener) {
        c.readStream.addNewMessageListener(listener);
    }
    
    private int[] list2Array(List<Integer> list){
        int tmpSize=  list.size();
        int[] arrRet = new int[tmpSize];        
        for(int i=0;i<tmpSize;i++){
            arrRet[i] = list.get(i);
        }
        
        return arrRet;
    }
    
    public void makeCoffee(int beans, int size, boolean milk, boolean hazelnut, boolean caramel){
        
        List<Integer> tmpList = new ArrayList<>();        
        tmpList.add(Constants.MAKE);
        tmpList.add(Constants.COFFEE);
        tmpList.add(beans);
        tmpList.add(size);
        
        if(milk){
            tmpList.add(Constants.MILK);
        }
        if(hazelnut){
            tmpList.add(Constants.HAZELNUT);
        }
        if(caramel){
            tmpList.add(Constants.CARAMEL);
        }
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    } 
    
    public void makeCappuccino(int beans, int size, boolean hazelnut, boolean caramel){
        List<Integer> tmpList = new ArrayList<>();        
        tmpList.add(Constants.MAKE);
        tmpList.add(Constants.CAPPUCCINO);
        tmpList.add(beans);
        tmpList.add(size);
        if(hazelnut){
            tmpList.add(Constants.HAZELNUT);
        }
        if(caramel){
            tmpList.add(Constants.CARAMEL);
        }
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public void makeEspresso(int size, boolean hazelnut, boolean caramel, boolean milk){
        List<Integer> tmpList = new ArrayList<>();        
        tmpList.add(Constants.MAKE);
        tmpList.add(Constants.ESPRESSO);
        tmpList.add(size);
        if(hazelnut){
            tmpList.add(Constants.HAZELNUT);
        }
        if(caramel){
            tmpList.add(Constants.CARAMEL);
        }
        if(milk){
            tmpList.add(Constants.MILK);
        }
        int[] send = list2Array(tmpList);       
        c.sendCommand(send);
    }
    
    public void makeGreenTea(int size, int temperature, int brewingTime){
        List<Integer> tmpList = new ArrayList<>(); 
        tmpList.add(Constants.MAKE);
        tmpList.add(Constants.GREEN_TEA);
        tmpList.add(size);
        tmpList.add(temperature);
        tmpList.add(brewingTime);
        int[] send = list2Array(tmpList);       
        c.sendCommand(send);
    }
    
    public void makeBlackTea(int size, int temperature, int brewingTime){
        List<Integer> tmpList = new ArrayList<>(); 
        tmpList.add(Constants.MAKE);
        tmpList.add(Constants.GREEN_TEA);
        tmpList.add(size);
        tmpList.add(temperature);
        tmpList.add(brewingTime);
        int[] send = list2Array(tmpList);       
        c.sendCommand(send);
    }
    
    public void makeFruitTea(int size, int temperature, int brewingTime){
        List<Integer> tmpList = new ArrayList<>(); 
        tmpList.add(Constants.MAKE);
        tmpList.add(Constants.GREEN_TEA);
        tmpList.add(size);
        tmpList.add(temperature);
        tmpList.add(brewingTime);
        int[] send = list2Array(tmpList);       
        c.sendCommand(send);
    }
    
    public void fillAll(){
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.FILL);
        tmpList.add(Constants.ALL);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public void fillCoffee(){
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.FILL);
        tmpList.add(Constants.ALL_COFFEE);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public void fillTea(){
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.FILL);
        tmpList.add(Constants.ALL_TEA);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public void fillExtras(){
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.FILL);
        tmpList.add(Constants.ALL_EXTRAS);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public  void emptyGarbage() {
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.EMPTY_GARBAGE);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public void cleanMachine(){
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.CLEAN_MACHINE);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public void quit(){
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.QUIT);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
    }
    
    public int getCaffein(int drink, int size) {
        answer = null;
        this.addNewMessageListener(new NewMessageListener() {
            @Override
            public void processNewMessage(NewMessageEvent event) {
                answer = event.getMessage();
                removeNewMessageListener(this);
            }
        });
        
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.GET);
        tmpList.add(Constants.CAFFEINE);
        tmpList.add(drink);
        tmpList.add(size);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
        
        while(answer == null) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        
        return answer.getCaffeine();
    }
    
    public int getPrice(int drink, int size) {
        answer = null;
        this.addNewMessageListener(new NewMessageListener() {
            @Override
            public void processNewMessage(NewMessageEvent event) {
                answer = event.getMessage();
                removeNewMessageListener(this);
            }
        });
        
        List<Integer> tmpList = new ArrayList<>();
        tmpList.add(Constants.GET);
        tmpList.add(Constants.PRICE);
        tmpList.add(drink);
        tmpList.add(size);
        int[] send = list2Array(tmpList);
        c.sendCommand(send);
        
        while(answer == null) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        
        return answer.getPrice();
    }
}